import request from '@/utils/request'
import type { 
  ApiResponse, 
  App, 
  CreateAppRequest, 
  AppListParams, 
  PageResponse,
  AppCategory 
} from '@/types/api'

// 创建应用
export const createApp = (data: CreateAppRequest): Promise<ApiResponse<App>> => {
  return request({
    url: '/app/create',
    method: 'post',
    data
  })
}

// 获取应用列表
export const getAppList = (): Promise<ApiResponse<App[]>> => {
  return request({
    url: '/app/list',
    method: 'get'
  })
}

// 获取应用详情
export const getAppDetail = (id: number): Promise<ApiResponse<App>> => {
  return request({
    url: `/app/${id}`,
    method: 'get'
  })
}

// 更新应用
export const updateApp = (id: number, data: Partial<CreateAppRequest>): Promise<ApiResponse<App>> => {
  return request({
    url: `/app/${id}`,
    method: 'put',
    data
  })
}

// 删除应用
export const deleteApp = (id: number): Promise<ApiResponse<null>> => {
  return request({
    url: `/app/${id}`,
    method: 'delete'
  })
}

// 发布应用
export const publishApp = (id: number): Promise<ApiResponse<null>> => {
  return request({
    url: `/app/${id}/publish`,
    method: 'post'
  })
}

// 下线应用
export const offlineApp = (id: number): Promise<ApiResponse<null>> => {
  return request({
    url: `/app/${id}/offline`,
    method: 'post'
  })
}

// 获取应用分类列表
export const getAppCategories = (): Promise<ApiResponse<AppCategory[]>> => {
  return request({
    url: '/app/categories',
    method: 'get'
  })
}

// 获取我的应用列表
export const getMyApps = (params?: AppListParams): Promise<ApiResponse<PageResponse<App>>> => {
  return request({
    url: '/app/my',
    method: 'get',
    params
  })
}

// 预聊天接口，获取初始的聊天内容
export const preChat = (appId: number): Promise<ApiResponse<string>> => {
  return request({
    url: '/app/preChat',
    method: 'get',
    params: { appId }
  })
}